package com.ruoyi.project.ledger.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.ledger.domain.LedgerFirefightingEquipment;
import com.ruoyi.project.system.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerManagementRegistrationMapper;
import com.ruoyi.project.ledger.domain.LedgerManagementRegistration;
import com.ruoyi.project.ledger.service.ILedgerManagementRegistrationService;

/**
 * 管理人员登记台账Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-14
 */
@Service
public class LedgerManagementRegistrationServiceImpl implements ILedgerManagementRegistrationService
{
    private static final Logger log = LoggerFactory.getLogger(LedgerManagementRegistrationServiceImpl.class);
    @Autowired
    private LedgerManagementRegistrationMapper ledgerManagementRegistrationMapper;

    /**
     * 查询管理人员登记台账
     *
     * @param id 管理人员登记台账ID
     * @return 管理人员登记台账
     */
    @Override
    public LedgerManagementRegistration selectLedgerManagementRegistrationById(Long id)
    {
        return ledgerManagementRegistrationMapper.selectLedgerManagementRegistrationById(id);
    }

    /**
     * 查询管理人员登记台账列表
     *
     * @param ledgerManagementRegistration 管理人员登记台账
     * @return 管理人员登记台账
     */
    @Override
    public List<LedgerManagementRegistration> selectLedgerManagementRegistrationList(LedgerManagementRegistration ledgerManagementRegistration)
    {
        return ledgerManagementRegistrationMapper.selectLedgerManagementRegistrationList(ledgerManagementRegistration);
    }

    /**
     * 新增管理人员登记台账
     *
     * @param ledgerManagementRegistration 管理人员登记台账
     * @return 结果
     */
    @Override
    public int insertLedgerManagementRegistration(LedgerManagementRegistration ledgerManagementRegistration)
    {
        ledgerManagementRegistration.setCreateTime(DateUtils.getNowDate());
        return ledgerManagementRegistrationMapper.insertLedgerManagementRegistration(ledgerManagementRegistration);
    }

    /**
     * 修改管理人员登记台账
     *
     * @param ledgerManagementRegistration 管理人员登记台账
     * @return 结果
     */
    @Override
    public int updateLedgerManagementRegistration(LedgerManagementRegistration ledgerManagementRegistration)
    {
        ledgerManagementRegistration.setUpdateTime(DateUtils.getNowDate());
        return ledgerManagementRegistrationMapper.updateLedgerManagementRegistration(ledgerManagementRegistration);
    }

    /**
     * 批量删除管理人员登记台账
     *
     * @param ids 需要删除的管理人员登记台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerManagementRegistrationByIds(Long[] ids)
    {
        return ledgerManagementRegistrationMapper.deleteLedgerManagementRegistrationByIds(ids);
    }

    /**
     * 删除管理人员登记台账信息
     *
     * @param id 管理人员登记台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerManagementRegistrationById(Long id)
    {
        return ledgerManagementRegistrationMapper.deleteLedgerManagementRegistrationById(id);
    }

    @Override
    public String importManagementRegistration(List<LedgerManagementRegistration> list, boolean isUpdateSupport, Long stationId) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入管理员登记台账不能为空！");
        }
        if (stationId == null){
            throw new CustomException("角色异常！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i = 1;
        for (LedgerManagementRegistration management : list){
            i++;
            try{
                if(management.getIdCard() == null || "".equals(management.equals(management.getIdCard()))){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行身份证号不能为空");
                    continue;
                }
                if(management.getName() == null || "".equals(management.equals(management.getName()))){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行姓名不能为空");
                    continue;
                }
                management.setStationId(stationId);

                LedgerManagementRegistration a = ledgerManagementRegistrationMapper.selectManagementRegistrationByIdCard(management);
                SysUser user = SecurityUtils.getLoginUser().getUser();
                if (StringUtils.isNull(a)){
                    management.setCreateId(user.getUserId().toString());
                    management.setCreateTime(DateUtils.getNowDate());
                    this.insertLedgerManagementRegistration(management);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、人员"+management.getName()+"导入成功");
                }
                else if (isUpdateSupport)
                {
                    management.setUpdateId(user.getUserId().toString());
                    management.setUpdateTime(DateUtils.getNowDate());
                    this.updateLedgerManagementRegistration(management);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、人员"+management.getName()+"更新成功");
                }

            }catch (Exception e){
                failureNum++;
                String msg = "<br/>第" + i + "行、人员"+management.getName()+"导入失败：";
                failureMsg.append(msg + "该行数据信息有误，请仔细检查");
                log.error(msg, e);
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String checkNameUnique(LedgerManagementRegistration ledgerManagementRegistration) {
        LedgerManagementRegistration registration = ledgerManagementRegistrationMapper.selectManagementRegistrationByIdCard(ledgerManagementRegistration);
        if (registration != null)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkNameUnique2(LedgerManagementRegistration ledgerManagementRegistration) {
        Long id = StringUtils.isNull(ledgerManagementRegistration.getId()) ? -1L : ledgerManagementRegistration.getId();
        LedgerManagementRegistration registration = ledgerManagementRegistrationMapper.selectManagementRegistrationByIdCard(ledgerManagementRegistration);
        if (StringUtils.isNotNull(registration) && registration.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
